<template>
  <div>
    <el-dialog
      :title="form.id ? '角色编辑' : '角色添加'"
      :visible.sync="bufferr.formBuffer"
    >
      {{ form }}
      <!-- 角色名称 -->
      <el-form :model="form">
        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input v-model="form.rolename"></el-input>
        </el-form-item>
        <!-- 角色权限 -->
        <el-tree
          :data="data"
          show-checkbox
          node-key="id"
          :props="defaultProps"
          @check="Rolecheck"
        >
        </el-tree>
        <!-- 角色状态 -->
        <!-- 菜单状态 -->
        <el-form-item label="角色状态" :label-width="formLabelWidth">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="bufferr.formBuffer = false">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: ["bufferr", "tableData"],
  data() {
    return {
      form: {
        rolename: "", //角色名称
        menus: "", //角色权限
        status: 1, //状态1启用 2禁用
      },
      formLabelWidth: "120px",
      defaultProps: {
        children: "children",
        label: "title",
      },
      data: [{}],
    };
  },
  watch: {
    bufferr: {
      handler(newV, oldV) {
        if (newV.formBuffer == false) {
          this.form = {
            rolename: "", //角色名称
         
            status: 1, //状态1启用 2禁用
            menus: [{}], //角色权限
          };
        }
      },
      deep: true, //使用深度监听
    },
  },
  mounted() {},
  created() {
    this.$api.reqGetMenu({ istree: true }).then((res) => {
    //   console.log(res);
      this.data = res;
    });
  },
  methods: {
    submit() {
      (this.form.id
        ? this.$api.reqEditRole(this.form)
        : this.$api.reqAddRole(this.form)
      )
        .then((res) => {
          console.log(res);
          // 3.1关闭弹窗
          this.bufferr.formBuffer = false;
          // 3.2提示用户操作成功
          this.$message({ type: "success", message: "操作成功！" });
          // 3.3赋空表单数据(使用了watch)
          // 3.4更新数据列表
          this.$emit("channel", "更新数据吧！");
        })
        .catch((err) => console.log(err));
    },
    Rolecheck(rolec, roleb) {
      console.log(rolec);
      console.log(roleb.checkedKeys.join(","));
    },
  },
};
</script>

<style lang="less" scoped>
</style>